package com.henry.db.migrate.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertyUtil {
		public static Map<String, Map<String, String>> propFileMap = new HashMap<String, Map<String, String>>();

		/**
		 * 从properties文件中获取指定属性名对应的�?
		 * 
		 * @param propertyFile
		 * @param propertyName
		 * @return
		 */
		public static String getProperty(String propertyFile, String propertyName) {
			if (propFileMap.get(propertyFile) == null
					|| StringUtil.isEmpty(propFileMap.get(propertyFile).get(
							propertyName))) {
				setCurPropFile(propertyFile);
			}

			return propFileMap.get(propertyFile).get(propertyName);
		}

		public static void setCurPropFile(String propertyFile) {
			Map<String, String> curMap = propFileMap.get(propertyFile);
			if(curMap==null){
				curMap=new HashMap<String, String>();
				propFileMap.put(propertyFile, curMap);
			}
				
			InputStream is = null;
			try {
				Properties propertie = new Properties();
				is = Thread.currentThread().getContextClassLoader()
						.getResourceAsStream(propertyFile);
				propertie.load(is);
				for (Object key : propertie.keySet()) {
					curMap.put((String) key, (String) propertie.get(key));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					is.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		/**
		 * 设置properties文件的某个属性�?
		 * 
		 * @param propertyFile
		 * @param propertyName
		 * @param propertyValue
		 * @return
		 */
		public synchronized static boolean setProperty(String propertyFile,
				String propertyName, String propertyValue) {
			boolean sav = false;
			InputStream is = null;
			OutputStream os = null;
			try {
				Properties propertie = new Properties();
				is = Thread.currentThread().getContextClassLoader()
						.getResourceAsStream(propertyFile);
				propertie.load(is);
				propertie.setProperty(propertyName, propertyValue);
				String path = Thread.currentThread().getContextClassLoader()
						.getResource(propertyFile).getPath();
				os = new FileOutputStream(new File(path));
				propertie.store(os, "");
				os.flush();
				sav = true;
				if (propFileMap.get(propertyFile) == null
						|| StringUtil.isEmpty(propFileMap.get(propertyFile).get(
								propertyName))) {
					setCurPropFile(propertyFile);
				}
				propFileMap.get(propertyFile).put(propertyName, propertyValue);
			} catch (Exception e) {
				e.printStackTrace();
				sav = false;
			} finally {
				try {
					is.close();
					os.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return sav;
		}

		public synchronized static boolean setPropertyValue(String propertyFile,
				String propertyName, String propertyValue) {
			return setProperty(propertyFile, propertyName, propertyValue);
		}

		private static Properties load(File f) throws IOException {
			Properties propertie = new Properties();
			InputStream is = new FileInputStream(f);
			propertie.load(is);
			is.close();
			return propertie;
		}

		public static Properties getProperties(String propertyFile)
				throws IOException {
			return load(new File(propertyFile));
		}

		public static String getPropertyValue(String propertyFile,
				String propertyName) {

			return getProperty(propertyFile, propertyName);
		}

		public static void main(String[] args) {
			String a = PropertyUtil.getPropertyValue("G:/ini/abc_xrtcfg.ini", "a");
			System.out.println(a);
		}
	}

